package com.app.hmst.main;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.hmst.model.IndicatorResultVO;


@SuppressLint("NewApi") public class GridViewImageForDeleteAdapter extends BaseAdapter {

	private Activity _activity;
	private List<String> menus;
	private int imageWidth;
	private ViewHolder viewHolder;
	private IndicatorResultVO indicatorResultVO;

	public GridViewImageForDeleteAdapter(Activity activity, List<String> menus,int imageWidth,IndicatorResultVO indicatorResultVO) {
		this._activity = activity;
		this.menus = menus;
		this.imageWidth = imageWidth;
		this.indicatorResultVO = indicatorResultVO;
	}

	@Override
	public int getCount() {
		return this.menus.size();
	}

	@Override
	public Object getItem(int position) {
		return this.menus.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWidth, imageWidth);
			
			convertView = inflater.inflate(R.layout.each_delete_box_layout, null);
			viewHolder.showCase = (ImageView) convertView.findViewById(R.id.showCase);
			viewHolder.delBtn = (ImageView) convertView.findViewById(R.id.imageView1);
			viewHolder.showCase.setLayoutParams(layoutParams);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		BitmapFactory.Options options = new BitmapFactory.Options();
//		options.inSampleSize = 8;

		byte[] decodedString = Base64.decode(menus.get(position), Base64.DEFAULT);
		Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length,options);
		
//		try {
//			ExifInterface ei = new ExifInterface(menus.get(position));
//			int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
//
//			switch(orientation) {
//			    case ExifInterface.ORIENTATION_ROTATE_90:
//			    	RotateBitmap(bitmap, 90);
//			        break;
//			    case ExifInterface.ORIENTATION_ROTATE_180:
//			    	RotateBitmap(bitmap, 180);
//			        break;
//			    // etc.
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
		
		
		viewHolder.showCase.setImageBitmap(bitmap);
		viewHolder.showCase.setOnClickListener(new OnShowImage(menus.get(position)));
		viewHolder.delBtn.setOnClickListener(new OnDeletedListener(position,convertView));
		
		return convertView;
	}
	 @Override
	  public void notifyDataSetChanged() {
	    super.notifyDataSetChanged();
	 }
	 public class OnShowImage implements OnClickListener{		
		private String _baseImage;
		public OnShowImage(String baseImage){
			this._baseImage = baseImage;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(_activity,ShowImageActivity.class);
			i.putExtra("imgBase64", _baseImage);
			_activity.startActivity(i);
		}
		 
	 }
	 public class OnDeletedListener implements OnClickListener{
			private int _position;
			private View view;
			public OnDeletedListener(int position,View btnView){
				this._position = position;
				this.view = btnView;
			}
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder1 = new AlertDialog.Builder(_activity);
		        builder1.setMessage(_activity.getString(R.string.error_delete));
		        builder1.setCancelable(true);
		        builder1.setPositiveButton(_activity.getString(R.string.homestay_sync_ok),new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		                menus.remove(_position);
						notifyDataSetChanged();
						switch (_position) {
						case 0:
							indicatorResultVO.img1=null;
						break;
						case 1:
							indicatorResultVO.img2=null;
						break;
						case 2:
							indicatorResultVO.img3=null;
						break;

						default:
							break;
						}
						indicatorResultVO.save();
		            }
		        });
		        builder1.setNegativeButton(_activity.getString(R.string.cancel_message), new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();		               
		            }
		        });
		        AlertDialog alert11 = builder1.create();
		        alert11.show();

			}
				
			
			
		}
	public static Bitmap RotateBitmap(Bitmap source, float angle)
	{
	      Matrix matrix = new Matrix();
	      matrix.postRotate(angle);
	      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
	}
	public class ViewHolder {
		private ImageView showCase,delBtn;
		private TextView titleImage;
		private RelativeLayout relative;
	}
}
